package queries;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login {
	private JFrame frame = new JFrame();
	public Login(){
		frame.setTitle("Login");
		frame.setLocation(300, 200);
		frame.setSize(new Dimension(500,300));
		JPanel panel = new JPanel();
		frame.add(panel);
		panel.setLayout(null);
		JLabel userLb = new JLabel("User:");
		userLb.setBounds(100, 40, 100, 30);
		JTextField userText = new JTextField();
		userText.setBounds(180, 40, 200, 30);
		panel.add(userLb);
		panel.add(userText);
		
		JLabel pasLb = new JLabel("Password:");
		pasLb.setBounds(100, 100, 100, 30);
		JPasswordField pas = new JPasswordField();
		pas.setBounds(180, 100, 200, 30);
		panel.add(pasLb);
		panel.add(pas);
		
		JButton reg = new JButton("Login");
		JButton reset = new JButton("Reset");
		reg.setBounds(120, 150, 100, 30);
		reset.setBounds(250, 150, 100, 30);
		panel.add(reg);
		panel.add(reset);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		reg.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					BufferedReader reader = new BufferedReader(new FileReader(new File("users.txt")));
					String tu = reader.readLine();
					String tp = reader.readLine();
					if(userText.getText().equals(tu)&&pas.getText().equals(tp)){
						frame.dispose();
						new MainFrame();
					}else{
						JOptionPane.showMessageDialog(null, "Username or password is incorrect!");
					}
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		reset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				userText.setText("");
				pas.setText("");
			}
		});
	}
	public static void main(String ... args){
		new Login();
	}
}
